package cn.sys.controller;

import cn.sys.controller.dto.SearchDTO;
import cn.sys.entity.ManagerInfo;
import cn.sys.entity.Notice;
import cn.sys.service.NoticeService;
import cn.sys.service.impl.NoticeServiceImpl;
import cn.sys.util.ParseRequestParameterUtil;
import cn.sys.util.ResponseUtil;
import cn.sys.util.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/notice")
public class NoticeServlet extends HttpServlet {
    private NoticeService noticeService = new NoticeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        System.out.println(action);

        switch (action){
            case "getNoticeList" : getNoticeList(req, resp); break;
            case "getManagerNoticeList": getManagerNoticeList(req, resp);break;
            case "deleteManagerNotice" : deleteManagerNotice(req, resp);break;
            case "getNoticeInfoById" : getNoticeInfoById(req, resp);break;
            case "addNotice" :addNotice(req, resp);break;
        }
    }

    /**
     * 新增公告
     * @param req
     * @param resp
     */
    private void addNotice(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 除了前端传来的公告，还需要管理员的id信息和当前时间
        HttpSession session = req.getSession();
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute("managerInfo");
        if(managerInfo == null){
            ResponseUtil.send(resp, ResultData.fail(ResultData.LOGIN_FAIL, "请先登录！"));
            return;
        }
        Date createTime = new Date();
        Notice notice = (Notice) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), Notice.class);
        notice.setManagerId(managerInfo.getId());
        notice.setCreateTime(createTime);
        // 将得到的对象传给业务层
        ResultData resultData = noticeService.addNotice(notice);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 公告详情查看
     * @param req
     * @param resp
     */
    private void getNoticeInfoById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer noticeId = Integer.valueOf(req.getParameter("noticeId"));
        ResultData resultData = noticeService.findDetailNotice(noticeId);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 删除管理员的公告
     * @param req
     * @param resp
     */
    private void deleteManagerNotice(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer noticeId = Integer.valueOf(req.getParameter("noticeId"));
        ResultData resultData = noticeService.deleteNotice(noticeId);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 获取当前登录的管理员的公告列表
     * @param req
     * @param resp
     */
    private void getManagerNoticeList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 从session中获取管理员信息，如果没有就直接返回
        HttpSession session = req.getSession();
        ManagerInfo managerInfo =(ManagerInfo) session.getAttribute("managerInfo");
        if(managerInfo == null){
            ResponseUtil.send(resp, ResultData.fail(ResultData.OPERATION_FAIL, "您还没有登录"));
            return;
        }
        // 取出管理员的id，封装到searchDTO中去
        SearchDTO searchDTO = new SearchDTO();
        searchDTO.setManagerId(managerInfo.getId());

        // 获取其他搜索信息
        searchDTO.setContent(req.getParameter("content"));
        searchDTO.setType(Integer.valueOf(req.getParameter("type")));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        try {
            if (!(startTime == null || "".equals(startTime))) {
                searchDTO.setStartTime(format.parse(startTime));
            }
            if (!(endTime == null || "".equals(endTime))) {
                searchDTO.setEndTime(format.parse(endTime));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
        // valueOf转为Integer类型，parseInt转为int类型
        searchDTO.setPageNum(Integer.valueOf(req.getParameter("currPage")));
        searchDTO.setPageSize(Integer.valueOf(req.getParameter("pageSize")));
        searchDTO.setOrderBy("create_time desc");
        ResultData resultData = noticeService.findNotices(searchDTO);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 获取公告列表功能
     * @param req
     * @param resp
     */
    private void getNoticeList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SearchDTO searchDTO = new SearchDTO();
        searchDTO.setContent(req.getParameter("content"));
        searchDTO.setType(Integer.valueOf(req.getParameter("type")));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        try {
            if (!(startTime == null || "".equals(startTime))) {
                searchDTO.setStartTime(format.parse(startTime));
            }
            if (!(endTime == null || "".equals(endTime))) {
                searchDTO.setEndTime(format.parse(endTime));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
        // valueOf转为Integer类型，parseInt转为int类型
        searchDTO.setPageNum(Integer.valueOf(req.getParameter("currPage")));
        searchDTO.setPageSize(Integer.valueOf(req.getParameter("pageSize")));
        searchDTO.setOrderBy("create_time desc");
        ResultData resultData = noticeService.findNotices(searchDTO);
        ResponseUtil.send(resp, resultData);
    }




}
